package com.laoxiang.modules.system.controller;

import com.laoxiang.modules.system.entity.SysRole;
import com.laoxiang.modules.system.entity.dto.PageDTO;
import com.laoxiang.modules.system.entity.vo.QueryRoleVO;
import com.laoxiang.modules.system.entity.vo.UpdateRoleVO;
import com.laoxiang.utils.AppHttpCodeEnum;
import com.laoxiang.utils.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import com.laoxiang.modules.system.service.SysRoleService;
import javax.annotation.Resource;
import java.util.List;

/**
 * 角色信息表(SysRole)表控制层
 *
 * @author makejava
 * @since 2023-07-13 20:51:02
 */
@Api(tags = "角色管理")
@RestController
@RequestMapping("/sysRole")
public class SysRoleController  {
    /**
     * 服务对象
     */
    @Resource
    private SysRoleService sysRoleService;

    @ApiOperation(httpMethod = "GET", value = "按条件查询角色列表")
    @GetMapping("/getRoleList")
    public ResponseResult getRoleList(Integer pageNum,Integer pageSize, QueryRoleVO queryRoleVO){
        PageDTO pageDTO = sysRoleService.getRoleList(pageNum, pageSize, queryRoleVO);
        return ResponseResult.okResult(pageDTO);
    }

    @ApiOperation(httpMethod = "GET", value = "获得所有角色列表")
    @GetMapping("/getAllRoles")
    public ResponseResult getAllRoles(){
        List<SysRole> roleList = sysRoleService.getRoleList();
        return ResponseResult.okResult(roleList);
    }

    @ApiOperation(httpMethod = "POST", value = "新增角色")
    @PostMapping("/saveRole")
    public ResponseResult saveRole(@RequestBody SysRole sysRole){
        int num = sysRoleService.saveRole(sysRole);
        if(num>0){
            return ResponseResult.okResult();
        }else{
            return ResponseResult.errorResult(AppHttpCodeEnum.ROLE_INSERT_ERROR);
        }

    }

    @ApiOperation(httpMethod = "PUT",value = "修改角色")
    @PutMapping("/editRole")
    public ResponseResult editRole(@RequestBody SysRole sysRole){
        int num = sysRoleService.updateRole(sysRole);
        if(num>0){
            return ResponseResult.okResult();
        }else{
            return ResponseResult.errorResult(AppHttpCodeEnum.ROLE_UPDATE_ERROR);
        }
    }


    @ApiOperation(httpMethod = "GET",value = "通过id获得角色信息")
    @GetMapping("/getSysRoleById/{roleId}")
    public ResponseResult getSysRoleById(@PathVariable("roleId") Long roleId){
        SysRole sysRoleById = sysRoleService.getSysRoleById(roleId);
        return ResponseResult.okResult(sysRoleById);
    }

    @ApiOperation(httpMethod = "DELETE",value = "通过id删除角色")
    @DeleteMapping("/deleteRoleById/{roleId}")
    public ResponseResult deleteRoleById(@PathVariable("roleId") Long roleId){
        int num = sysRoleService.deleteRoleById(roleId);
        if(num>0){
            return ResponseResult.okResult();
        }else{
            return ResponseResult.errorResult(AppHttpCodeEnum.ROLE_DELETE_ERROR);
        }
    }

    @ApiOperation(httpMethod = "DELETE",value = "批量删除角色")
    @DeleteMapping("/batchDeleteRole")
    public ResponseResult batchDeleteRole(@RequestBody List<Long> roleIds){
        sysRoleService.batchDeleteRole(roleIds);
        return ResponseResult.okResult();
    }

    @ApiOperation(httpMethod = "PUT",value = "修改角色状态")
    @PutMapping("/updateRoleStatus")
    public ResponseResult updateRoleStatus(@RequestBody UpdateRoleVO updateRoleVO){
        sysRoleService.updateRoleStatus(updateRoleVO);
        return ResponseResult.okResult();
    }

}

